<?php

declare (strict_types=1);

namespace app\http\controller;


use app\api\model\Category;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Lib\Gateway;
use think\facade\Log;
use Workerman\Worker;
use think\facade\Cache;
use Workerman\Lib\Timer;


class Events
{

    public static function onWorkerStart(Worker $businessWorker)
    {
        //Cache::store('redis')->flushall();
        //进程启动清除所有定时器
        Timer::delAll();
    }

    public static function onConnect($client_id)
    {
        echo "用户【{$client_id}】来了" . PHP_EOL;
    }

    public static function onMessage($client_id, $message)
    {
        echo $message . PHP_EOL;
        $message = json_decode($message,true);
        if(!empty($message['type'])){
            $type = $message['type'] ?? '';
            $bag = new Bag();
            if($type == 'login'){
                $bag->enterPage($message,$client_id);
            }elseif($type == 'joinQueue'){
                $bag->joinQueue($message,$client_id);
            }elseif($type == 'leaveFreeRoom'){
                $bag->leaveFreeRoom($message,$client_id);
            }elseif($type == 'pong'){
                $bag->pong($client_id);
            }
        }
        return true;
    }

    public static function onClose($client_id)
    {
        echo "用户【{$client_id}】走了" . PHP_EOL;
        $bag = new Bag();
        $bag->closeConnect($client_id);

    }

    public static function onWorkerStop(BusinessWorker $businessWorker)
    {

    }

}